package com.liwalo.dao;

// Generated 24-ago-2013 14:46:30 by Hibernate Tools 3.6.0

import static org.hibernate.criterion.Example.create;

import java.util.List;

import javax.naming.InitialContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import com.liwalo.entities.Categoria;
import com.liwalo.entities.Ciudad;
import com.liwalo.entities.Provincia;
import com.liwalo.entities.Subcategoria;
import com.liwalo.entities.TiendaProducto;
import com.liwalo.utils.HibernateUtil;

/**
 * Home object for domain model class Subcategoria.
 * @see com.liwalo.dao.Subcategoria
 * @author Hibernate Tools
 */
public class SubcategoriaDAO {

	private static final Log log = LogFactory.getLog(SubcategoriaDAO.class);

	private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

	protected SessionFactory getSessionFactory() {
		try {
			return (SessionFactory) new InitialContext()
					.lookup("SessionFactory");
		} catch (Exception e) {
			log.error("Could not locate SessionFactory in JNDI", e);
			throw new IllegalStateException(
					"Could not locate SessionFactory in JNDI");
		}
	}

	public void persist(Subcategoria transientInstance) {
		log.debug("persisting Subcategoria instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Subcategoria instance) {
		log.debug("attaching dirty Subcategoria instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Subcategoria instance) {
		log.debug("attaching clean Subcategoria instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Subcategoria persistentInstance) {
		log.debug("deleting Subcategoria instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Subcategoria merge(Subcategoria detachedInstance) {
		log.debug("merging Subcategoria instance");
		try {
			Subcategoria result = (Subcategoria) sessionFactory
					.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Subcategoria findById(java.lang.Integer id) {
		log.debug("getting Subcategoria instance with id: " + id);
		try {
			Subcategoria instance = (Subcategoria) sessionFactory
					.getCurrentSession().get("com.liwalo.dao.Subcategoria", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Subcategoria> findByExample(Subcategoria instance) {
		log.debug("finding Subcategoria instance by example");
		try {
			List<Subcategoria> results = (List<Subcategoria>) sessionFactory
					.getCurrentSession()
					.createCriteria("com.liwalo.dao.Subcategoria")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public List<Subcategoria> finByCategoriaList(Subcategoria subcategoriavo ) {
		log.debug("finding Subcategoria instance by example");
		Transaction tx =null;
		try {
			
			Session openSession = sessionFactory.openSession();
			tx = openSession.beginTransaction();
			tx.begin();
			
			List<Subcategoria> results = (List<Subcategoria>) openSession
					.createCriteria(Subcategoria.class)
					.createAlias("categoriaId", "categoria")
					.add(Restrictions.eq("categoria.id", subcategoriavo.getCategoriaId().getId()))
					.add(Restrictions.eq("estado",1))
					.list();
			log.debug("finByCategoriaList, result size: "
					+ results.size());
			tx.commit();
			return results;
		} catch (Exception re) {
			log.error("Error while loading category list", re);
			if(null!= tx){
				tx.rollback();
			}
			return null;
		}
	}
	
	
	public List<Categoria> listadoCategoriasTienda(TiendaProducto tiendaprovo) {
		try {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(TiendaProducto.class,"tp")
				.createCriteria("tp.categoriaId","categoriaid")
				.createCriteria("tp.subcategoriaId","subcategoriaid")
				.add(Restrictions.eq("subcategoriaid.estado", 1))
				.add(Restrictions.eq("tp.tiendaId",tiendaprovo.getTiendaId()))
				.add(Restrictions.eq("categoriaid.estado",1)
				
						
						);
		return (List<Categoria>) criteria.list();
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public List<Subcategoria> listadoSubCategoriasTienda(TiendaProducto tiendaprovo) {
		try {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(TiendaProducto.class,"tp")
				.createCriteria("tp.categoriaId","categoriaid")
				.createCriteria("tp.subcategoriaId","subcategoriaid")
				
				.add(Restrictions.eq("tp.categoriaId", tiendaprovo.getCategoriaId()))
				.add(Restrictions.eq("tp.tiendaId",tiendaprovo.getTiendaId()))
				.add(Restrictions.eq("subcategoriaid.estado", 1))
				.add(Restrictions.eq("categoriaid.estado",1)
						
				);
		return (List<Subcategoria>) criteria.list();
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Subcategoria> getAllByCategoria(Categoria instance) {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Subcategoria.class)
								   .add(Restrictions.eq("categoriaId.id", instance.getId()))
								   .add(Restrictions.eq("estado",1));
		return (List<Subcategoria>) criteria.list();
	}
}
